import mcp
import httpx
from mcp.server.fastmcp import FastMCP

# 创建MCP服务器实例
app = FastMCP()

# 定义并注册工具函数
@app.tool()
def web_search(query: str) -> str:
    """
    使用Bing搜索引擎进行网络搜索，并返回摘要结果。
    """
    response = httpx.get(
        "https://api.bing.microsoft.com/v7.0/search",
        params={"q": query},
        headers={"Ocp-Apim-Subscription-Key": "your_api_key"}
    )
    response.raise_for_status()
    data = response.json()
    snippets = [item["snippet"] for item in data["webPages"]["value"]]
    return "\n".join(snippets)

# 启动MCP服务器，使用stdio传输层
if __name__ == "__main__":
    app.run(transport="stdio")